﻿using CoreModule;
using Microsoft.Extensions.Logging;
using Prism.Mvvm;
using Prism.Regions;
namespace UIModule.ViewModels
{
    /// <summary>
    /// 测量 首页
    /// </summary>
    public class MeasurementHomeViewModel : 
        BindableBase,Prism.Navigation.IDestructible
        ,IRegionMemberLifetime,INavigationAware
    {
        IRegionManager RegionManager { get; set; }
        IDeviceManager DeviceManager { get; set; }
        IConfigurationManager ConfigurationManager { get; set; }
        ILogger Logger { get; set; }
        public bool KeepAlive => false;
        public MeasurementHomeViewModel(
            ILogger logger,
            IConfigurationManager configurationManager,
            IRegionManager regionManager,
            IDeviceManager deviceManager,
            IWindowLifetime windowLifetime
            )
        {
            Logger= logger;
            ConfigurationManager = configurationManager;
            RegionManager = regionManager;
            DeviceManager = deviceManager;
            deviceManager.RecognitionResultCompleted += DeviceManager_RecognitionResultCompleted;
            deviceManager.MeasurementResultCompleted += DeviceManager_MeasurementResultCompleted;
           
            windowLifetime.RegionViewKind = RegionViewKind.MeasurementHome;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeviceManager_MeasurementResultCompleted(object sender, MeasurementResultInfoEventArgs e)
        {
            if (e.Cancel)
            {
                return;
            }
            //空包 不处理
            if (e.MeasurementResultKind== MeasurementResultKind.None)
            {
                return;
            }
           ///其他 有值 跳转到 测量界面
            var navigationParameters = new NavigationParameters{
                { 
                        CoreConstant.MeasurementResultInfoEventArgsKey, e }
                };
             RegionManager.RequestNavigate(CoreConstant.MainRegion, UIConstant.MeasurementResultView, navigationParameters);

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeviceManager_RecognitionResultCompleted(object sender, RecognitionResultInfoEventArgs e)
        {
            if (e.RecognitionResultInfo.IsEmpty || e.Cancel)
            {
                return;
            }
             ///收到 患者信息
            var navigationParameters = new NavigationParameters
                {
                    { CoreConstant.RecognitionResultInfoKey, e.RecognitionResultInfo }
                };
            RegionManager.RequestNavigate(CoreConstant.MainRegion, UIConstant.MeasurementResultView, navigationParameters);
        }
          
        /// <summary>
        /// 
        /// </summary>
        public void Destroy()
        {
            DeviceManager.RecognitionResultCompleted -= DeviceManager_RecognitionResultCompleted;
            DeviceManager.MeasurementResultCompleted -= DeviceManager_MeasurementResultCompleted;
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            CoreModule.PatientInfo.Instance = null;
            return;
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true ;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            return;
        }
    }
}
